package com.conan.app.dailyflickr.app;

import java.util.concurrent.RejectedExecutionException;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.AsyncTask;

import com.conan.app.dailyflickr.App;
import com.conan.app.dailyflickr.AppData;
import com.conan.app.dailyflickr.app.ImageDownloadTask.ImageDownloadListener;


public class BitmapHttpGet {
    private ImageDownloadListener mListener;
    private Context _context;
    public class QueueItem{
        int widgetId;
        int position;
        String id;
        int page;

        public QueueItem(int widgetId, int page, int position, String id) {
            this.widgetId = widgetId;
            this.position = position;
            this.id = id;
            this.page = page;
        }
    }

    public BitmapHttpGet(Context context, ImageDownloadListener listener){
        _context = context;
        mListener = listener;
    }	

    public void fetchBitmapOnThread(int widgetId, int page, String id, String urlString, int position) {	
        Bitmap bitmap = null;

        if(position < 0){
            bitmap = ApplicationUtils.getBitmapFile(_context, 
                    String.format(AppData.IMAGE_PATTERN, widgetId, -1));
        }else{
            bitmap = ApplicationUtils.getBitmapFile(_context, 
                    String.format(AppData.IMAGE_PATTERN, page, id));
        }


        if(bitmap != null){
            mListener.completed(widgetId, position, bitmap);

            return;
        }

        // mQueueMap.put(urlString, 
        // 		new QueueItem(widgetId, page, position, id));
        startDownload(position, urlString, new QueueItem(widgetId, page, position, id));



    }

    public int getRequests(int widgetId){
        if(getImageTask != null){
            return getImageTask.getRequests(widgetId);
        }
        return 0;
    }	

    /**
     * clear old request
     */
    public void clearRequestBitmap(int widgetId){
        if(getImageTask != null){
            getImageTask.clearRequestBitmap(widgetId);
        }
    }

    /**
     * clear request bitmap for main view
     */
    public void clearMainBitmapItem(String url){
        if(getImageTask != null){
            getImageTask.clearMainBitmapItem(url);
        }	
    }

    ImageDownloadTask getImageTask;
    /**
     * start download task
     * @param position
     * @param urlString
     * @param queueItem
     */
    private void startDownload(int position, String urlString, QueueItem queueItem){
        if(getImageTask != null && 
                (getImageTask.getStatus() == AsyncTask.Status.RUNNING ||
                getImageTask.isRunning)){
            getImageTask.putToDownload(position, urlString, queueItem);
            getImageTask.setListener(mListener);
        }else{
            getImageTask = new ImageDownloadTask(
                    App.getContext(), 
                    position, urlString, queueItem);
            getImageTask.setListener(mListener);
            try{
                getImageTask.execute();				
            }catch (RejectedExecutionException e) {	
                e.printStackTrace();
            }
        }
    }

    /**
     * stop download task
     */
    public void stopDownload(){		
        if(getImageTask != null && getImageTask.getStatus() == AsyncTask.Status.RUNNING ){
            getImageTask.cancel(true);
        }
        getImageTask = null;
    }

    /**
     * set flag of connectivity to stop download task when no internet 
     * @param connected
     */
    public void changeConnectionInternet(boolean connected){
        if(getImageTask != null){
            getImageTask.setInternetConnection(connected);
        }
    }

}
